<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/5/17
 * Time: 16:17
 */

namespace app\data;

use app\common\Common;
use think\Db;

class OrderData extends Base
{
    public function addOrder($shop_id, $user_id, $goods_id, $goods_price, $money, $goods_attr, $total_integral, $number,
                             $user_get_coupon, $address, $user_msg, $freight, $freight_price, $return_integral)
    {
        $time = time();
        $order_num = Common::getOrderNumber('o', 10, $time);
        Db::table('drh_goods_order')
            ->insert([
                'shop_id' => $shop_id,
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'goods_attr_value' => $goods_attr,
                'order_num' => $order_num,
                'goods_price' => $goods_price,
                'goods_num' => $number,
                'total_price' => $goods_price * $number,
                'price' => $money,
                'total_integral' => $total_integral,
                'user_msg' => $user_msg,
                'address' => $address,
                'user_get_coupon' => $user_get_coupon,
                'goods_freight' => $freight,
                'freight_price' => $freight_price,
                'order_start_time' => $time,
                'return_integral' => $return_integral,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功', 'order_num' => $order_num];
    }


    public function show($shop_id, $goods_status, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_goods_order a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'a.user_id',
                'a.order_num',
                'a.freight_num',
                'b.goods_name',
                'a.freight_method',
                'a.goods_status',
                'a.address'
            ])->where([
                'a.shop_id' => $shop_id,
                'a.goods_status' => $goods_status
            ])->order($sort)
            ->limit($page)
            ->select();
    }

    public function ship($order_num, $freight_method, $freight_num)
    {
        $time = time();
        Db::table('drh_goods_order')
            ->where("order_num='{$order_num}' and order_end_time > 0 and goods_status=0")
            ->update([
                'freight_method' => $freight_method,
                'freight_num' => $freight_num,
                'goods_status' => 1,
                'ship_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '发送成功'];
    }

    public function editFreightNum($order_num, $freight_method, $freight_num)
    {
        Db::table('drh_goods_order')
            ->where("order_num='{$order_num}' and order_end_time > 0 and goods_status=0")
            ->update([
                'freight_method' => $freight_method,
                'freight_num' => $freight_num,
                'goods_status' => 1,
            ]);
        return ['code' => $this->t_code, 'msg' => '发送成功'];
    }

    /**
     * 获取数量
     * @param $where
     * @param $table
     * @return mixed
     */
    public function total($where, $table = 'drh_goods_order')
    {
        $res = Db::table($table)
            ->field('count(*) count')
            ->where($where)
            ->find();
        return $res['count'];
    }

    public function getOrderInfo($order_num)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'a.goods_id',
                'a.shop_id',
                'a.user_id',
                'a.price',
                'a.goods_num',
                'a.freight_price',
                'a.total_integral',
                'b.proxy_goods_id',
                'b.id',
                'a.return_integral',
            ])->where([
                'order_num' => $order_num
            ])->find();
    }

    public function endOrder($order_num, $pay_status, $time)
    {

        Db::table('drh_goods_order')
            ->where([
                'order_num' => $order_num
            ])
            ->update([
                'pay_status' => $pay_status,
                'order_end_time' => $time,
            ]);
        return true;
    }

    public function payEndOrder($order_num, $pay_status, $time)
    {
        Db::table('drh_user_pay')
            ->where([
                'order_num' => $order_num
            ])
            ->update([
                'pay_status' => $pay_status,
                'end_time' => $time,
            ]);
        return true;
    }

    public function showUserOrder($where, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');

        return Db::table('drh_goods_order a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->join('drh_shop c', 'a.shop_id=c.id', 'left')
            ->join('drh_goods d', 'a.goods_id=d.id', 'left')
            ->join('drh_goods_comment e', 'a.order_num=e.order_num', 'left')
            ->field([
                'a.id',
                'a.order_num',
                'd.goods_img',
                'd.goods_name',
                'd.goods_price',
                'c.shop_name',
                'a.goods_num',
                'a.price',
                'a.freight_price',
                'a.pay_status',
                'a.total_integral',
                'a.goods_status',
                'IFNULL(e.lv,0) lv',
            ])->where($where)->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showUserOrderInfo($id, $open_id, $order_num)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->join('drh_shop c', 'a.shop_id=c.id', 'left')
            ->join('drh_goods d', 'a.goods_id=d.id', 'left')
            ->field([
                'a.id',
                'a.shop_id',
                'd.goods_img',
                'd.goods_name',
                'd.goods_price',
                'c.shop_name',
                'a.goods_num',
                'a.price',
                'a.goods_freight',
                'a.freight_price',
                'a.pay_status',
                'a.total_integral',
                'a.goods_status',
                'a.address',
                "FROM_UNIXTIME('a.order_start_time','%Y-%m-%d %H-%i-%s') create_time",
                "FROM_UNIXTIME('a.order_end_time','%Y-%m-%d %H-%i-%s') end_time",
                'a.freight_method',
                'a.freight_num',
                'a.ship_time',
                'a.receipt_time',
                'a.order_num',
                'd.give_integral'
            ])->where([
                'a.id' => $id,
                'b.user_open_id' => $open_id,
                'a.order_num' => $order_num
            ])->find();
    }

//    public function shopFlow($order_num, $time, $shop_info, $type_status, $money_expenses, $integral_expenses,
//                             $fees, $msg = '')
//    {
//        Db::table('drh_shop_flow')
//            ->insert([
//                'shop_id' => $shop_info['id'],
//                'start_money' => $shop_info['money'],
//                'start_integral' => $shop_info['integral'],
//                'order_num' => $order_num,
//                'type_status' => $type_status,
//                'money_expenses' => $money_expenses,
//                'integral_expenses' => $integral_expenses,
//                'fees' => $fees,
//                'end_money' => $shop_info['money'] + $money_expenses,
//                'end_integral' => $shop_info['integral'] + $integral_expenses,
//                'create_time' => $time,
//                'msg' => $msg
//            ]);
//        return true;
//    }

    public function showOrderNum($where, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods_order')
            ->field([
                'order_num'
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showOrderInfo($order_num)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'a.order_num',
                'b.goods_name',
                'a.total_price',
                'a.goods_freight',
                'a.goods_num',
                'a.address',
                'a.price',
                'a.total_integral',
                'a.user_msg',
                'a.goods_attr_value',
                'a.goods_status',
                'a.freight_num',
                "FROM_UNIXTIME(a.ship_time,'%Y-%m-%d %H:%i:%s') ship_time",
                "FROM_UNIXTIME(a.receipt_time,'%Y-%m-%d %H:%i:%s') receipt_time",
                "FROM_UNIXTIME(a.order_end_time,'%Y-%m-%d %H:%i:%s') order_end_time",
                'a.user_get_coupon'
            ])->where([
                'order_num' => $order_num
            ])->find();
    }

    public function showHotGoods($pre)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'b  .goods_name',
                'sum(a.goods_num) count',
                'a.goods_id',
                'b.goods_img',
                'b.shop_id'
            ])->where('a.order_end_time > 0 and a.goods_status !=3')
            ->group('a.goods_id')
            ->order('count desc')
            ->limit(0, $pre)
            ->select();
    }

    public function showProxyGoods($pre)
    {
        return Db::table('drh_goods a')
            ->join('drh_goods b', 'b.id=a.proxy_goods_id')
            ->field([
                'b.goods_name',
                'count(*) count',
                'b.id',
                'b.goods_img'
            ])->where('a.proxy_goods_id > 0 ')
            ->group('b.id')
            ->order('count desc')
            ->limit(0, $pre)
            ->select();
    }

    public function showRatingsGoods($pre)
    {
        return Db::table('drh_goods_comment a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'b.goods_name',
                'AVG(lv) lv',
                'a.goods_id',
                'b.goods_img'
            ])->where('lv > 0')
            ->group('a.goods_id')
            ->order('lv desc')
            ->limit(0, $pre)
            ->select();
    }


    public function showReturnsGoods($pre)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'b.goods_name',
                'count(*) count',
                'a.goods_id',
                'b.goods_img'
            ])->where('a.goods_status = 3 and a.order_end_time >0 ')
            ->group('a.goods_id')
            ->order('count desc')
            ->limit(0, $pre)
            ->select();
    }

    /**
     * 营业额
     * @param $id
     * @return mixed
     */
    public function getTurnover($id)
    {
        $res = Db::table('drh_goods_order')
            ->field([
                'sum(total_price) total_price'
            ])->where("shop_id={$id} and order_end_time >0 and goods_status !=3")
            ->find();
        return $res['total_price'];
    }

    /**
     * 分佣金额
     * @param $id
     * @return mixed
     */
    public function getCommission($id)
    {
        $res = Db::table('drh_shop_flow')
            ->field([
                'sum(money_expenses) money'
            ])->where("shop_id={$id} and money_expenses > 0  and type_status = 4 ")
            ->find();
        return $res['money'];
    }

    public function getOrderNum($start_time, $end_time)
    {
        return Db::table('drh_goods_order a')
            ->join('drh_delivery_address b', 'a.address_id=b.id', 'left')
            ->join('drh_platform_flow c', 'a.order_num=c.order_num', 'left')
            ->field([
                'a.order_num',
                'c.money_expenses money',
                'b.address_province province',
                'b.address_municipal municipal',
                'b.address_district district',
            ])->where("
            a.order_end_time>={$start_time} 
            and 
            a.order_end_time <{$end_time} 
            and 
            a.goods_status !=3 
       
            ")
            ->select();
    }

//////////////////----------------------------------------v3--------------------------------------////////////


    public function showOrderList($page, $pre, $sort, $where)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_flow a')
            ->join('drh_goods_order b', 'a.order_num=b.order_num', 'left')
            ->join('drh_shop c', 'b.shop_id=c.id', 'left')
            ->join('drh_user_info_app d', 'a.user_id=d.user_id', 'left')
            ->field([
                'd.user_head_img',
                "IFNULL(c.shop_name,'平台') shop_name",
                "FROM_UNIXTIME('a.create_time','%Y-%m-%d %H-%i-%s') create_time",
                'a.integral_expenses'
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function addPayOrder($shop_id, $user_id, $total_integral, $money, $integral, $return_ratio, $time, $order_num)
    {
        return Db::table('drh_user_pay')
            ->insertGetId([
                'order_num' =>$order_num,
                'shop_id'=>$shop_id,
                'user_id'=>$user_id,
                'total_price'=>$total_integral*10+$money,
                'price'=>$money,
                'total_integral'=>$total_integral,
                'return_integral'=>$integral,
                'return_integral_value'=>$return_ratio,
                'create_time'=>$time
                ]);
    }

    public function getPayOrderInfo($order_num)
    {
        return Db::table('drh_user_pay')
            ->field([
                'shop_id',
                'user_id',
                'total_price',
                'price',
                'total_integral',
                'return_integral'
            ])->where([
                'order_num'=>$order_num
            ])->find();
    }

    public function endPayOrder($order_num, $pay_status, $time)
    {
        return Db::table('drh_user_pay')
            ->where([
                'order_num'=>$order_num
            ])->update([
                'pay_status'=>$pay_status,
                'end_time'=>$time
            ]);
    }


    public function showOrderPayInfo($order_num)
    {
        return Db::table('drh_user_pay a')
            ->join('drh_shop b','a.shop_id=b.id','left')
            ->field([
                'b.shop_name',
                'a.price',
                "FROM_UNIXTIME('a.end_time','%Y-%m-%d %H-%i-%s') create_time",
                "a.total_integral use_integral",
                'a.return_integral',
                'a.order_num',
                'a.pay_status'
            ])->where([
                'order_num'=>$order_num
            ])->find();
    }


}